﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sinacor.Infra.Core.ProcessManager.Server;
using PSE.ProcessManager.Server.InteligenceModules;
using PSE.ProcessManager.Common.Gateway;
using PSE.ProcessManager.Common.Process;

namespace Sinacor.Process.Bovespa.Distribuicao.Server.ProcessImplementation
{
    public class ProcessamentoImportacao : SinacorBaseProcessServer<ProcessLogic.ProcessamentoImportacao>, ISimpleExecutionProcess
    {
        #region Construtor

        /// <summary>
        /// Construtor padrão
        /// </summary>
        public ProcessamentoImportacao()
        {
            this.Id = ProcessEntity.Catalog.ProcessamentoImportacao.ProcessID;
            this.Name = "ProcessamentoImportacao";

            this.ProcessLogic.ProcessamentoConcluido += new EventHandler(EnviarMensagemProcessamentoConcluido);
        }

        #endregion

        #region Event Handlers

        private void EnviarMensagemProcessamentoConcluido(object sender, EventArgs e)
        {
            this.SendMessage(new Message()
            {
                ProcessId = this.Id,
                MessageId = ProcessEntity.Catalog.ProcessamentoImportacao.ProcessamentoConcluido.MessageID
            });
        }

        #endregion

        #region Métodos

        /// 
        /// <param name="message"></param>
        public void ProcessarDistribuicao(BaseMessage message)
        {
            bool fail = false;

            try
            {
                //while (!System.Diagnostics.Debugger.IsAttached)
                //    System.Threading.Thread.Sleep(1000);

                if (!IsAborted)
                {
                    this.ProcessLogic.ProcessarDistribuicao(message.RetrieveParameter<ProcessEntity.Parameter.FiltroProcessamentoDistribuicao>());
                }
            }
            catch (Exception ex)
            {
                fail = true;
                HandleException(ex);
            }
            finally
            {
                Finalize(fail);
            }
        }

        #endregion

        #region ISimpleExecutionProcess Members

        public void Execute(BaseMessage message)
        {
            this.Finalize();
        }

        #endregion
    }
}
